const app = getApp()
const db = wx.cloud.database()
Component({
  /**
   * 组件的属性列表
   */
  options: {
    styleIsolation: 'apply-shared'
  },
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    isFocus: false,
    historyList: [],
    searchList: [],
    text: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //光标聚焦搜索框时
    focus(){
      wx.getStorage({
        key: 'history',
        success:(res)=>{
          this.setData({
            historyList:res.data
          })
        }
      })
     this.setData({
       isFocus: true
    })
    },
    //点击取消时
    blur(event){
      this.setData({
        isFocus: false,
        searchList: [],
        text: ''
      })
    },

    //点击回车时监听
    confirm(event){
      let cloneList = [...this.data.historyList]
      const vau = event.detail.value
      cloneList.unshift(vau)
      console.log(event)
      wx.setStorage({
        data: [...new Set(cloneList)],
        key: 'history',
      })
      this.getList(vau)
    },

    //删除历史记录
    delet(){
      wx.removeStorage({
        key: 'history',
        success:(res)=>{
          this.setData({
            historyList:[]
          })
        }
      })
    },

    //通过历史记录查询
    history(event){
      console.log(event)
      const btn = event.target.dataset.btn
      this.getList(btn)
    },

    //获取数据库信息
    getList(vau){
      db.collection('news').where({
        nickName: db.RegExp({
          regexp: vau,
          options: 'i'
        })
      }).field({
        nickName: true,
        avater: true
      }).get().then((res)=>{
        const list = [...res.data]
        console.log(list)
        this.setData({
          searchList: list
        })
      })
    },
  }
})
